/*
* jlb
*/
package com.jlb.miner.ctrl;

import com.alibaba.fastjson.JSON;
import com.jlb.core.entity.BeanRet;
import com.jlb.core.entity.Page;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.miner.entity.MinerItem;
import com.jlb.miner.entity.MinerItemState;
import com.jlb.miner.service.MinerItemSV;
import com.jlb.user.entity.UserMiner;
import com.jlb.user.service.UserMinerSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 矿机 控制器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/minerItem")
@Slf4j
@Api(value = "矿机控制器", description = "矿机控制器")
public class MinerItemCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private MinerItemSV minerItemSV;

    @Resource
    private UserMinerSV userMinerSV;

    /**
     * 查询MinerItem一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询MinerItem一个详情信息", notes = "查询MinerItem一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(java.lang.Long id, java.lang.String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        MinerItem minerItem = minerItemSV.load(id, code);
        log.info(JSON.toJSONString(minerItem));
        return BeanRet.create(true, "查询成功", minerItem);
    }


    /**
     * 根据条件id查询MinerItem一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询MinerItem一个详情信息", notes = "查询MinerItem一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable java.lang.Long id) {
        if (id == null) {
            return null;
        }
        MinerItem minerItem = minerItemSV.loadById(id);
        log.info(JSON.toJSONString(minerItem));
        return BeanRet.create(true, "查询成功", minerItem);
    }

    /**
     * 根据条件code查询MinerItem一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询MinerItem一个详情信息", notes = "查询MinerItem一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable java.lang.String code) {
        if (code == null) {
            return null;
        }
        MinerItem minerItem = minerItemSV.loadByCode(code);
        log.info(JSON.toJSONString(minerItem));
        return BeanRet.create(true, "查询成功", minerItem);
    }


    /**
     * 查询MinerItem信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询MinerItem信息集合", notes = "查询MinerItem信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore MinerItem minerItem, Integer curPage, Integer pageSize) {
        Page<MinerItem> page = new Page<MinerItem>(pageSize, curPage);
        List<MinerItem> minerItems = minerItemSV.list(minerItem, page.genRowStart(), page.getPageSize());
        page.setVoList(minerItems);
        int total = minerItemSV.count(minerItem);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);

    }


    /**
     * 查询MinerItem信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询MinerItem信息集合", notes = "查询MinerItem信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(java.lang.Long id, java.lang.String code, Integer curPage, Integer pageSize) {
        Page<MinerItem> page = new Page<MinerItem>(pageSize, curPage);
        List<MinerItem> minerItems = minerItemSV.list(id, code, page.genRowStart(), page.getPageSize());
        page.setVoList(minerItems);
        int total = minerItemSV.count(id, code);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);
    }

    /**
     * 统计MinerItem信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计MinerItem信息数量根据主键", notes = " 统计MinerItem信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(java.lang.Long id, java.lang.String code) {
        return minerItemSV.count(id, code);
    }

    /**
     * 统计MinerItem信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计MinerItem信息数量", notes = "统计MinerItem信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "price", value = "价格", paramType = "query"),
            @ApiImplicitParam(name = "dailyIncome", value = "日收益", paramType = "query"),
            @ApiImplicitParam(name = "power", value = "算力", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：待上架 Audit，销售中 Sales，已下架 Withdraw", paramType = "query"),
            @ApiImplicitParam(name = "upperLimit", value = "挖矿上限", paramType = "query"),
            @ApiImplicitParam(name = "multiple", value = "上限倍数", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore MinerItem minerItem) {
        if (minerItem == null) {
            return minerItemSV.count(new HashMap());
        } else {
            return minerItemSV.count(minerItem);
        }
    }


    /**
     * 创建MinerItem
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建矿机", notes = "创建矿机")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "price", value = "价格", paramType = "query"),
            @ApiImplicitParam(name = "dailyIncome", value = "日收益", paramType = "query"),
            @ApiImplicitParam(name = "power", value = "算力", paramType = "query"),
            @ApiImplicitParam(name = "upperLimit", value = "挖矿上限", paramType = "query"),
            @ApiImplicitParam(name = "separateLimit", value = "挖矿分割到商城上线", paramType = "query"),
            @ApiImplicitParam(name = "multiple", value = "上限倍数", paramType = "query"),
            @ApiImplicitParam(name = "inventory", value = "库存", paramType = "query"),
            @ApiImplicitParam(name = "jlb", value = "金兰宝单买价格", paramType = "query"),
            @ApiImplicitParam(name = "active", value = "金兰宝单买激活币价格", paramType = "query"),
            @ApiImplicitParam(name = "mixJlb", value = "混合买金兰宝价格", paramType = "query"),
            @ApiImplicitParam(name = "mixOre", value = "混合买矿石价格", paramType = "query"),
            @ApiImplicitParam(name = "mixActive", value = "混合买激活币价格", paramType = "query"),
            @ApiImplicitParam(name = "isActiveCoin", value = "是否需要激活币 Y,N", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore MinerItem minerItem) {
        int price = Integer.valueOf(minerItem.getPrice());
        /*if (price != (minerItem.getJlb() + minerItem.getActive())) {
            return BeanRet.create(false, "金兰宝+激活币不等于价格");
        }
        if (price != (minerItem.getMixJlb() + minerItem.getMixOre() + minerItem.getMixActive())) {
            return BeanRet.create(false, "金兰宝+矿石+激活币不等于价格");
        }*/
        minerItemSV.save(minerItem);
        return BeanRet.create(true, "创建成功", minerItem);
    }


    /**
     * 修改MinerItem
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改MinerItem", notes = "修改MinerItem")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "price", value = "价格", paramType = "query"),
            @ApiImplicitParam(name = "dailyIncome", value = "日收益", paramType = "query"),
            @ApiImplicitParam(name = "power", value = "算力", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：待上架 Audit，销售中 Sales，已下架 Withdraw", paramType = "query"),
            @ApiImplicitParam(name = "upperLimit", value = "挖矿上限", paramType = "query"),
            @ApiImplicitParam(name = "multiple", value = "上限倍数", paramType = "query"),
            @ApiImplicitParam(name = "inventory", value = "库存", paramType = "query"),
            @ApiImplicitParam(name = "jlb", value = "金兰宝单买价格", paramType = "query"),
            @ApiImplicitParam(name = "active", value = "金兰宝单买激活币价格", paramType = "query"),
            @ApiImplicitParam(name = "mixJlb", value = "混合买金兰宝价格", paramType = "query"),
            @ApiImplicitParam(name = "mixOre", value = "混合买矿石价格", paramType = "query"),
            @ApiImplicitParam(name = "mixActive", value = "混合买激活币价格", paramType = "query"),
            @ApiImplicitParam(name = "isActiveCoin", value = "是否需要激活币 Y,N", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore MinerItem minerItem) {
        minerItemSV.modify(minerItem);
        if (StringUtils.isNotBlank(minerItem.getMultiple())) {
            if (Float.parseFloat(minerItem.getMultiple()) > 0) {
                Map<String, Object> param = new HashMap<>();
                param.put("minerCode", minerItem.getCode());
                List<UserMiner> userMiners = userMinerSV.list(param);
                userMiners.forEach(userMiner -> {
                    userMiner.setMultiple(minerItem.getMultiple());
                    userMinerSV.modify(userMiner);
                });
            }
        }


        return BeanRet.create(true, "修改成功", minerItem);
    }


    /**
     * 修改状态
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：待上架 Audit，销售中 Sales，已下架 Withdraw", paramType = "query"),
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, MinerItemState state) {
        minerItemSV.updateState(code, state);
        return BeanRet.create(true, "状态修改成功");
    }


    /**
     * 用户查询到的矿机
     *
     * @return 分页对象
     */
    @ApiOperation(value = "用户查询到的矿机", notes = "用户查询到的矿机")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/listForUser")
    @ResponseBody
    public BeanRet listForUser(@ApiIgnore MinerItem minerItem, Integer curPage, Integer pageSize) {
        Page<MinerItem> page = new Page<MinerItem>(pageSize, curPage);
        minerItem.setState(MinerItemState.Sales.name());
        List<MinerItem> minerItems = minerItemSV.list(minerItem, page.genRowStart(), page.getPageSize());
        page.setVoList(minerItems);
        int total = minerItemSV.count(minerItem);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);

    }

}
